import axios from "axios";
axios.default.withCredentials = true
    // 创建axios实例
const rest = axios.create({
    baseURL: "http://localhost:3000/",
    timeout: 5000,
})

// request(请求)拦截器
rest.interceptors.request.use(config => {
    // 提交文件类型
    let token = sessionStorage.getItem('token')
    if (config.data instanceof FormData) {
        config.headers = {
            'Content-Type': 'multipart/form-data',
            'Authorization': token
        }
    } else {
        // 常规请求
        console.log(config);
        if (config.url == "/login") {
            config.headers = {
                'Content-Type': 'application/json; charset=utf-8',
                // 'Content-Type': 'application/x-www-form-urlencoded',
            }
        } else {
            config.headers = {
                'Content-Type': 'application/json; charset=utf-8',
                // 'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': token
            }
        }
    }

    config.timeout = 10000
    return config
}, error => {
    Promise.reject(error)
})

// respone(响应)拦截器
rest.interceptors.response.use(
    response => {
        if (response.status == 200) {
            return response.data
        }

    }, error => {}
)
export default rest